package com.interceptor;

import com.entity.Student;
import com.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Controller
public class MyInterceptor implements HandlerInterceptor {
    @Autowired
    private StudentService studentService;
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("方法执行之前执行:preHandle");
       Student student = new Student();
       student.setName(request.getParameter("name"));
       student.setAge(Integer.valueOf(request.getParameter("age")));
//       查询数据库看是否有这样这个人
        Student student1 = studentService.queryOne(student);
        System.out.println(student1);
        boolean flag=false;
       if (student1!=null){
           flag=true;
       }
        return flag;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

        System.out.println("方法执行之后执行:postHandle");
        modelAndView.addObject("myname","余志鹏");
       modelAndView.setViewName("show");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("方法最总执行:afterCompletion");


    }
}
